<?php

class Advertise_model extends Model {
	
	private $table 	= 'advertise';
	
	function Advertise_model() {
		parent::Model ();
	}
	
	function add_banner() {
		
		$image = isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '';
		
		$data = array(			
			'place'			=> isset( $_POST['place'] ) ? (int)$_POST['place'] : 0,
			'image'			=> $image,
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',
			'image_type'	=> substr( $image, -3 ),
			'image_width'	=> isset( $_POST['width'] ) ? (int)$_POST['width'] : 0,
			'image_height'	=> isset( $_POST['height'] ) ? (int)$_POST['height'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'link'			=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : 0,
			'description'	=> isset( $_POST['description'] ) ? htmlspecialchars( $_POST['description'] ) : 0,
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'orders'		=> 0,
			'date'			=> time()
		);
		
		$this->db->insert( $this->table , $data );	
	}
	
	function update_banner() {
		
		$image = isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '';
		
		$data = array(			
			'place'			=> isset( $_POST['place'] ) ? (int)$_POST['place'] : 0,
			'image'			=> $image,
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',
			'image_type'	=> substr( $image, -3 ),
			'image_width'	=> isset( $_POST['width'] ) ? (int)$_POST['width'] : 0,
			'image_height'	=> isset( $_POST['height'] ) ? (int)$_POST['height'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'link'			=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : 0,
			'description'	=> isset( $_POST['description'] ) ? htmlspecialchars( $_POST['description'] ) : 0,
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0
		);
		$id = isset( $_POST['hid'] ) ? (int)$_POST['hid'] : 0;
		
		$this->db->where('id', $id );	
		$this->db->update( $this->table, $data);	
	}
	
	function list_banner( $place = 0, $num , $offset, &$total ) {
		$data = array ();
		
		$query = $this->db->query("SELECT `id`, `place`, `image`, `image_text`, `image_type`, `image_width`, `image_height`, `name`, `link`, `description`, `status`, `orders` FROM `$this->table` WHERE `place` = '$place' ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();		
		
		// Tông số bản ghi
		$query = $this->db->query("SELECT `id` FROM $this->table WHERE `place` = '$place' ");
		$total = $query->num_rows();
		
		$query->free_result ();
		return $data;		
	}
	
	function update_banner_status( $total = 0, $place = 0 ) {
	
		for( $k = 1; $k < $total ; $k++ ) {
			
			$status = @(int)$_POST['status_'.$k];
			$order 	= @(int)$_POST['order_'.$k];
			
			$id		= @(int)$_POST['id_'.$k]; 
			
			$data = array(
				'status'	=> $status,
				'orders'	=> $order,
			);
			
			$this->db->where('id', $id );	
			$this->db->where('place', $place );
			
			$this->db->update( $this->table, $data);
		}
	}
	
	function select_banner( $id = 0) {
		$data = array ();
		
		if ( is_numeric( $id ) ) {
			$query = $this->db->query("SELECT `id`, `place`, `image`, `image_text`, `image_type`, `image_width`, `image_height`, `name`, `link`, `description`, `status`, `orders` FROM `$this->table` WHERE `id` = '$id' LIMIT 1 ");
			
			if ( $query->num_rows () > 0 ) {
				$data = $query->row_array();
			}
			$query->free_result ();
		}
		return $data;
	}	
	
	function del_banner( $id ) {
		$this->db->where('id', $id );
		$this->db->delete( $this->table );
	}
}
?>